package com.uottawa.library.service;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.uottawa.library.dao.LoanPeriodDAO;
import com.uottawa.library.domain.LoanPeriod;
import com.uottawa.library.domain.User;

public class LoanPeriodService {

	private LoanPeriodDAO loanPeriodDAO = new LoanPeriodDAO();
	
	public int getCountOfRenewals()
	{
		return loanPeriodDAO.getCountLoanPeriod();
	}
	public int getMaxRenewals()
	{
		return loanPeriodDAO.getCountLoanPeriod()-1;
	}
	

	

	
	
	public boolean isAdmin(User user)
	{
		if (user.getRole() == null)
			return false;
		else
			return user.getRole().equalsIgnoreCase("admin");
		
	}
	
	
	public String addPeriod(LoanPeriod loanPeriod, User user) {

		if (isAdmin(user)) {
			int maxId = loanPeriodDAO.getMaxLoanPeriodId();
			loanPeriod.setPeriodId(maxId + 1);
			loanPeriodDAO.addLoanPeriod(loanPeriod);
			return "added";
		} else
			return "not added";
		
	}


	public LoanPeriod getLoanPeriod(int i) {
		return loanPeriodDAO.getLoanPeriod(i);
	}

	public String setLoanPeriodDuration(LoanPeriod loanPeriod, User user) {
		if (isAdmin(user)) {
			loanPeriodDAO.updateLoanPeriod(loanPeriod);
			return "updated";
		} else
			return "not updated";
		
		
	}

	public void deleteLoanPeriod(LoanPeriod loanPeriod, User user) {
		if (isAdmin(user)) 
			loanPeriodDAO.deleteLoanPeriod(loanPeriod);

		
	}
	public java.util.List<LoanPeriod> getAllLoanPeriod() {
		return loanPeriodDAO.getAllLoanPeriod();
	}
	public String removeLoanPeriod(int loanPeriodId) {
		boolean removed = loanPeriodDAO.deleteLoanPeriod(loanPeriodId);
				String result = (removed) ? "success" : "failed";
		return result;

	}
	public String updateLoanPeriod(LoanPeriod loanPeriod) {
		boolean updated = loanPeriodDAO.updateLoanPeriod(loanPeriod);
		String result = (updated) ? "success" : "failed";
return result;
	}
	

}
